#!/usr/bin/env bash
export PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

# Zabbix Install  For 5.0.5
# Installation Zabbix Monitoring System Script
# Author: Mybbsky
# Last update 17/11/2020
# Thank you for using Zabbix script

#关闭防火墙及selinux
setenforce 0
#添加防火墙端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
#防火墙重新载入
firewall-cmd --reload
#暂停防火墙
systemctl  stop firewalld
# install
IP=`ip add|grep global|awk -F'[ /]+' '{ print $3 }'|head -n 1` 

echo '------------Your IP address---------' 
#ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}'
printf $IP"\n";
echo '------------completed---------' 
echo '===============================Zabbix5.0.5====================================='
echo 'Installation Zabbix Monitoring System Script 					 '
echo 'Author: Mybbsky									 '
echo 'Last update 17/11/2020         					 '
echo '==============================================================================='

read  -p "Input  yes to install(yes/n): " yes;

if [ "$yes" == 'n' ];then
		exit;
	fi

yum install wget -y
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum clean all
yum install zabbix-server-mysql zabbix-agent  -y
yum install centos-release-scl  -y
#修改YUM配置
sed -i  's/enabled=0/enabled=1/g'   /etc/yum.repos.d/zabbix.repo
yum install -y rh-php72-php-gd rh-php72-php-bcmath rh-php72-php-mbstring rh-php72-php-mysqlnd rh-php72-php-xml rh-nginx116-nginx rh-php72 rh-php72-php-fpm rh-php72-php-ldap
yum install zabbix-web-mysql-scl zabbix-nginx-conf-scl  -y
yum install  mariadb-server   -y
#安装脚本解释器
yum install -y expect
yum install -y git
echo '=================================Zabbix========================================'
echo ' 				INSTALL	 DBSERVER    OK , DO YOU  WANT START DBSERVER ?			 '
echo '==============================================================================='

read  -p "Input  yes to Next (yes/n): " y;

if [ "$yes" == 'n' ];then
		exit;
	fi
	
systemctl start mariadb
systemctl enable mariadb


echo '=================================Zabbix========================================'
echo ' 					DBSERVER START OK ,   INITIALIZE DATABASE 	                 '
echo '==============================================================================='

curl -O https://gitee.com/mybbsky_admin_admin/zabbix_onkey/raw/master/auto_mysql.sh  && chmod +x auto_mysql.sh && expect auto_mysql.sh Abc.123+

echo '=================================Zabbix========================================'
echo ' 				INSTALL	 DBSERVER    OK , DO YOU  WANT START DBSERVER ?			 '
echo '==============================================================================='

read  -p "Input  yes to Next (yes/n): " y;

echo '=================================Zabbix========================================'
echo ' 	INITIALIZE DATABASE OK , create database zabbix & create user zabbix		 '
echo '==============================================================================='

read  -p "Input  yes to Next (yes/n): " y;

if [ "$yes" == 'n' ];then
		exit;
	fi
echo '==============================================================================='	
echo ' 	YOUR DBSERVER ROOT PASSWORD		create database zabbix                       '	
echo '==============================================================================='
mysql -uroot -pAbc.123+ -e "create database zabbix character set utf8 collate utf8_bin;"   
#设置utf编码以便支持中文数据
echo '==============================================================================='	
echo ' 	YOUR DBSERVER ROOT PASSWORD		create user zabbix                           '	
echo '==============================================================================='
mysql -uroot -pAbc.123+ -e "create user zabbix@localhost identified by 'zabbix';"             
#创建zabbix用户， by'密码'
echo '==============================================================================='	
echo ' 	YOUR DBSERVER ROOT PASSWORD		grant all privileges on zabbix               '	
echo '==============================================================================='
mysql -uroot -pAbc.123+ -e "grant all privileges on zabbix.* to zabbix@localhost;"             
#授权zabbix用户登陆范围 @localhost 本地


echo '=================================Zabbix========================================'
echo ' IMPORT	DATABASE	'	
echo ' YOUR DBSERVER Zabbix PASSWORD	 (default:zabbix)	'	
echo '==============================================================================='
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
echo ' SETTING ZABBIX SERVER CONFING FILES'	
sed -i '/^# DBPassword=.*/c  DBPassword=zabbix' /etc/zabbix/zabbix_server.conf
echo ' SETTING PHP TIMEZONE CONFING	'	
#去注释
sed -i  's/; //' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
#改PHP时区
sed -i 's#Europe/Riga#Asia/Shanghai#' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
#添加PHP配置
sed -i  's/# //' /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
#配置
sed -i  '33{s/fastcgi_pass/#fastcgi_pass/}' /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
#添加nginx配置
sed -i  's/listen.acl_users = apache/listen.acl_users = apache,nginx/g'  /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
#添加中文支持
sed -i  's/graphfont/simhei/g'  /usr/share/zabbix/include/defines.inc.php
#下载字体修改NGINX默认网站为zabbix
cd /home
git clone https://gitee.com/mybbsky_admin_admin/zabbix_onkey.git
cd zabbix_onkey
mv /etc/opt/rh/rh-nginx116/nginx/nginx.conf /etc/opt/rh/rh-nginx116/nginx/nginx.conf.bak
cp nginx.conf  /etc/opt/rh/rh-nginx116/nginx
cp simhei.ttf /usr/share/zabbix/assets/fonts
#删除下载的文件
rm -rf /home/zabbix_onkey
#启动zabbix服务
echo '=================================Zabbix========================================'
echo '					RESTART  ALL  SERVICES , PLEASE WAIT						 '
echo '==============================================================================='
#启用服务
systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
#更改selinux设置
sed -i  's/SELINUX=enforcing/SELINUX=permissive/g'  /etc/selinux/config
echo '=================================Zabbix========================================'
echo ' 			install ok, tank you for use zabbix install scripts					 '
echo '==============================================================================='

printf "Server IP is:"$IP"\n";
printf "Zabbix Url: http://hostname/\n";
printf "hostname config file is /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf\n"；
printf "Mysql user:root  password is: Abc.123+ \n"；
printf "Web HostName is: example.com \n"；
#最后可以再修改数据库的root密码
#mysqladmin -u root -password 你的密码
#修改数据库的root密码
#mysqladmin -u root -p 你的密码 password 你的新密码
echo '=================================Zabbix========================================'
echo ' 								Install   End									 '
echo '==============================================================================='
#
#2021年8月17日最后修改
#
#
#最后可以再修改数据库的root密码
#mysqladmin -u root -password 你的密码
#修改数据库的root密码
#mysqladmin -u root -p 你的密码 password 你的新密码
#
#
#
#